package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsPolicyStatisticsInfo;
import com.hxqh.bo.dao.FpsPolicyStatisticsInfoDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsPolicyStatisticsInfo entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsPolicyStatisticsInfo
 * @author MyEclipse Persistence Tools
 */

public class FpsPolicyStatisticsInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory
			.getLog(FpsPolicyStatisticsInfoDAO.class);
	// property constants
	public static final String POLICY_ID = "policyId";
	public static final String ALL_TRADE_NUM = "allTradeNum";
	public static final String USER_CHOOSE_NUM = "userChooseNum";
	public static final String MAX_HIGH_LOST = "maxHighLost";
	public static final String PROFIT_LOST = "profitLost";
	public static final String RETURN_RATE = "returnRate";
	public static final String INITIAL_FUND = "initialFund";

	protected void initDao() {
		// do nothing
	}

	public void save(FpsPolicyStatisticsInfo transientInstance) {
		log.debug("saving FpsPolicyStatisticsInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(FpsPolicyStatisticsInfo transientInstance) {
		log.debug("updating FpsPolicyStatisticsInfo instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(FpsPolicyStatisticsInfo persistentInstance) {
		log.debug("deleting FpsPolicyStatisticsInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsPolicyStatisticsInfo findById(java.lang.Integer id) {
		log.debug("getting FpsPolicyStatisticsInfo instance with id: " + id);
		try {
			FpsPolicyStatisticsInfo instance = (FpsPolicyStatisticsInfo) getHibernateTemplate()
					.get("com.hxqh.bo.bean.FpsPolicyStatisticsInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public FpsPolicyStatisticsInfo getByPolicyId(final Integer policyId) {
		log.debug("getting FpsPolicyStatisticsInfo instance with policyId: " + policyId);
		Integer id = (Integer) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql = "select POLICY_STATISCS_ID from fps_policy_statistics_info where POLICY_ID = " + policyId;
				SQLQuery q = arg0.createSQLQuery(sql);
				q.addScalar("POLICY_STATISCS_ID", Hibernate.INTEGER);
				
				return q.uniqueResult();
			}
		});
		return id!=null ? findById(id) : null;
	}

	public List findByExample(FpsPolicyStatisticsInfo instance) {
		log.debug("finding FpsPolicyStatisticsInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsPolicyStatisticsInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FpsPolicyStatisticsInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPolicyId(Object policyId) {
		return findByProperty(POLICY_ID, policyId);
	}

	public List findByAllTradeNum(Object allTradeNum) {
		return findByProperty(ALL_TRADE_NUM, allTradeNum);
	}

	public List findByUserChooseNum(Object userChooseNum) {
		return findByProperty(USER_CHOOSE_NUM, userChooseNum);
	}

	public List findByMaxHighLost(Object maxHighLost) {
		return findByProperty(MAX_HIGH_LOST, maxHighLost);
	}

	public List findByProfitLost(Object profitLost) {
		return findByProperty(PROFIT_LOST, profitLost);
	}

	public List findByReturnRate(Object returnRate) {
		return findByProperty(RETURN_RATE, returnRate);
	}

	public List findByInitialFund(Object initialFund) {
		return findByProperty(INITIAL_FUND, initialFund);
	}

	public List findAll() {
		log.debug("finding all FpsPolicyStatisticsInfo instances");
		try {
			String queryString = "from FpsPolicyStatisticsInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FpsPolicyStatisticsInfo merge(
			FpsPolicyStatisticsInfo detachedInstance) {
		log.debug("merging FpsPolicyStatisticsInfo instance");
		try {
			FpsPolicyStatisticsInfo result = (FpsPolicyStatisticsInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsPolicyStatisticsInfo instance) {
		log.debug("attaching dirty FpsPolicyStatisticsInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsPolicyStatisticsInfo instance) {
		log.debug("attaching clean FpsPolicyStatisticsInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsPolicyStatisticsInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsPolicyStatisticsInfoDAO) ctx
				.getBean("FpsPolicyStatisticsInfoDAO");
	}
}